package sdk.webview.fmc.com.fmcsdk.check.adapter;


import android.graphics.Color;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.check.CacheHelper;
import sdk.webview.fmc.com.fmcsdk.check.bean.GetDetailsBean;
import sdk.webview.fmc.com.fmcsdk.imagepicker.utils.Utils;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.GsonUtil;
import sdk.webview.fmc.com.fmcsdk.util.StringUtils;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;

public class CheckDetailsAdapter extends BaseQuickAdapter<GetDetailsBean.RecordDTO.PointsDTO, BaseViewHolder> {

    private boolean isReadonly;
    private GetDetailsBean detailsBean;
    private int readonly;


    private OnItemChildClickListener listener;
    public CheckDetailsAdapter(int layoutResId) {
        super(layoutResId);
    }

    public void setReadonly(boolean isReadonly, GetDetailsBean detailsBean, int readonly){
        this.isReadonly = isReadonly;
        this.detailsBean = detailsBean;
        this.readonly = readonly;
    }

    public void setListener(OnItemChildClickListener listener){
        this.listener = listener;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, GetDetailsBean.RecordDTO.PointsDTO item) {
        holder.setText(R.id.tv_name,isReadonly ? (holder.getAdapterPosition()+1)+"、"+item.getAssetDesc() : item.getAssetDesc());
        holder.setGone(R.id.tv_name, !StringUtils.isEmpty(item.getAssetDesc()));
        holder.setText(R.id.tv_content, isReadonly ? (holder.getView(R.id.tv_name).getVisibility() == View.VISIBLE ? item.getLocationDesc() : (holder.getAdapterPosition()+1)+"丶"+ item.getLocationDesc()): item.getLocationDesc());
        holder.setGone(R.id.tv_content, !StringUtils.isEmpty(item.getLocationDesc()));

        if (isReadonly){
            holder.getView(R.id.ll_check).setVisibility(View.GONE);
            holder.getView(R.id.ll_readonly).setVisibility(View.VISIBLE);
            holder.getView(R.id.iv_go).setVisibility(View.GONE);
        }else {
            holder.getView(R.id.ll_check).setVisibility(View.VISIBLE);
            holder.getView(R.id.ll_readonly).setVisibility(View.GONE);
            holder.getView(R.id.iv_go).setVisibility(View.VISIBLE);
        }




        if (isReadonly){
            if (!StringUtils.isEmpty(item.getWorkorderId()) && !item.getWorkorderId().equals("0")){
                holder.getView(R.id.ll_check_the_report).setVisibility(View.VISIBLE);
            }else {
                holder.getView(R.id.ll_check_the_report).setVisibility(View.INVISIBLE);
            }

            holder.getView(R.id.ll_check_the_report).setOnClickListener(v ->{
                if (holder.getView(R.id.ll_check_the_report).getVisibility() == View.VISIBLE){
                    String baseUrl = PreferenceManager.getDefaultSharedPreferences(mContext).getString(Constant.BASE_ADDRESS_URL, "");
                    String h5Url = baseUrl+"/h5/mytask/?accessToken="+ UserHelper.getToken() + "#/reportSrtask/detail/"+item.getWorkorderId()+"?from=app";
                    if (null != listener){
                        listener.childClick(h5Url);
                    }
                }
            });

            if (item.getStatus().equals("3")){
                if (null !=item.getFinishDate()){
                    holder.getView(R.id.tv_time).setVisibility(View.VISIBLE);
                    holder.setText(R.id.tv_time,mContext.getResources().getString(R.string.time)+ Utils.getTimeHM(item.getFinishDate()));
                }else {
                    holder.getView(R.id.tv_time).setVisibility(View.GONE);
                }


                boolean hasError = false;
                if (null != item.getCheckList() && !item.getCheckList().isEmpty()){
                    for (int i = 0; i < item.getCheckList().size(); i++) {
                        if (item.getCheckList().get(i).getCheckResult().equals("N1")){
                            hasError = true;
                            break;
                        }
                    }
                }


                if (item.getAbnormal().equals("1")){
                    holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.Abnormal));
                    holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#FB8852"));
                }else {
                    if (hasError){
                        holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.Abnormal));
                        holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#FB8852"));
                    }else {
                        holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.Normal));
                        holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#32CB6F"));
                    }
                }
            }else {
                if (detailsBean.getRecord().getStatus().equals("INIT")
                        || (readonly == 1 && detailsBean.getRecord().getStatus().equals("ALLOCATED"))
                        || (readonly == 1 && detailsBean.getRecord().getStatus().equals("ING"))
                        || (readonly == 1) && detailsBean.getRecord().getStatus().equals("SUSPEND")){
                    if (item.getStatus().equals("1")){
                        holder.getView(R.id.tv_time).setVisibility(View.GONE);
                        holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.unCheck));
                        holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#F1484B"));
                    }else {
                        holder.getView(R.id.tv_time).setVisibility(View.VISIBLE);
                        holder.setText(R.id.tv_time,mContext.getResources().getString(R.string.time)+ Utils.getTimeHM(item.getFinishDate()));


                        boolean hasError = false;
                        if (null != item.getCheckList() && !item.getCheckList().isEmpty()){
                            for (int i = 0; i < item.getCheckList().size(); i++) {
                                if (item.getCheckList().get(i).getCheckResult().equals("N1")){
                                    hasError = true;
                                    break;
                                }
                            }
                        }

                        if (item.getAbnormal().equals("1")){
                            holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.Abnormal));
                            holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#FB8852"));
                        }else {
                            if (hasError){
                                holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.Abnormal));
                                holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#FB8852"));
                            }else {
                                holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.Normal));
                                holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#32CB6F"));
                            }
                        }



                    }

                }else {
                    holder.getView(R.id.tv_time).setVisibility(View.GONE);
                    holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.Missed));
                    holder.setTextColor(R.id.tv_not_check_status,Color.parseColor("#F1484B"));

                    if (null != detailsBean){
                        String checkRange = detailsBean.getRecord().getCheckRange();
                        int range = Integer.parseInt(checkRange);
                        if (detailsBean.getRecord().getCheckPercent() > 0 && range == 2){
                            holder.setText(R.id.tv_not_check_status,mContext.getResources().getString(R.string.unCheck));
                        }
                    }
                }
            }

        }else {
            String textStatus = "";
            if (TextUtils.equals(item.getStatus(), "3")) {
                textStatus = mContext.getResources().getString(R.string.Inspected);
                holder.setTextColor(R.id.tv_status, Color.parseColor("#32CB6F"));
            } else if (TextUtils.equals(item.getStatus(), "1")) {
                textStatus = "";
            } else if (TextUtils.equals(item.getStatus(), "2")) {
                textStatus =  mContext.getResources().getString(R.string.Partially_Inspected);
                holder.setTextColor(R.id.tv_status, Color.parseColor("#FB8852"));
            }
            if (item.getIsSkip() == 1){
                textStatus = "";
            }
            holder.setText(R.id.tv_status,textStatus);
            holder.setGone(R.id.tv_status, !StringUtils.isEmpty(textStatus));

            if (item.getMustScan().equals("1") || item.getIsSkip() == 1){
                holder.getView(R.id.ll_scan).setVisibility(View.VISIBLE);
                if (item.getIsSkip() == 1){
                    holder.setText(R.id.tv_scan, mContext.getResources().getString(R.string.Skipped));
                }else if (item.getMustScan().equals("1") && !CacheHelper.getInstance().getPointScanMarkers(item.getId())){ //需要扫码 并且当前点位是未扫的状态
                    holder.setText(R.id.tv_scan, mContext.getResources().getString(R.string.Need_to_scan_code));
                }else {
                    holder.getView(R.id.ll_scan).setVisibility(View.GONE);
                }
                if (item.getIsSkip() == 1){
                    holder.setText(R.id.tv_scan, mContext.getResources().getString(R.string.Skipped));
                }
                holder.getView(R.id.view_line).setVisibility(StringUtils.isEmpty(textStatus) ? View.GONE : View.VISIBLE);
            }else {
                holder.getView(R.id.ll_scan).setVisibility(View.VISIBLE);
                if (item.getMustScan().equals("2")|| item.getIsSkip() == 1){
                    if (item.getIsSkip() == 1){
                        holder.setText(R.id.tv_scan, mContext.getResources().getString(R.string.Skipped));
                    } else if (item.getMustScan().equals("2") && !CacheHelper.getInstance().getPointScanMarkers(item.getId())){
                        holder.setText(R.id.tv_scan, mContext.getResources().getString(R.string.NFC_required));
                    }else {
                        holder.getView(R.id.ll_scan).setVisibility(View.GONE);
                    }

                    if (item.getIsSkip() == 1){
                        holder.setText(R.id.tv_scan, mContext.getResources().getString(R.string.Skipped));
                    }
                    holder.getView(R.id.view_line).setVisibility(StringUtils.isEmpty(textStatus) ? View.GONE : View.VISIBLE);
                }else {
                    holder.getView(R.id.ll_scan).setVisibility(View.GONE);
                }

            }
        }

        holder.setVisible(R.id.view_bottom_line, holder.getAdapterPosition() != getData().size() - 1);
    }


    public interface OnItemChildClickListener{
        void childClick(String url);
    }

}
